//
//  MCManagerBackgroundViewController.swift
//  StoreManage
//
//  Created by MC on 2018/5/17.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCManagerBackgroundViewController: MCViewController {


    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc override func leftItemClicked() {
        self.navigationController?.popViewController(animated: true)
    }

    
    @objc func scanButtonClicked() {
        let vc = MCSweepPayViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    
    @objc func billButtonClicked() {
        let vc = MCBillDetailViewController()
        let date = NSDate()
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "yyyy-MM-dd"
        let strNowTime = timeFormatter.string(from: date as Date) as String        
        vc.dateStr = strNowTime

        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }

    @objc func switchEvent() {
        var closeBroadcast = 0
        if isPlaySwitch.isOn {
            closeBroadcast = 0
        } else {
            closeBroadcast = 1
        }
        
        
        let para = [
            "closeBroadcast" : closeBroadcast,
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_updateCloseBroadcast, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            
            var str = ""
            if closeBroadcast == 1 {
                str = "成功关闭语音播报"
            } else {
                str = "成功开启语音播报"
            }
            
            MCHUD_text(message: str, view: self.view, delay: 1.2)
            
        }, dislike: { (response) in }) { (error) in }
        
    }
    
    
    
    // MARK: - Network
    func sendNetworking() {
        let para = [:] as [String : Any]
        
        MCNetwork_post(url: JAVA_findBackstageInfo, para: para as [String : AnyObject], vc: self,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            let dataDict = response["data"] as? [String : AnyObject] ?? [:]
            let money = dataDict["money"] as? NSNumber ?? 0
            let closeBroadcast = dataDict["closeBroadcast"] as? NSNumber ?? 0  // 是否关闭语音播报

            let storeName = dataDict["storeName"] as? String ?? ""
            let nickName = dataDict["nickName"] as? String ?? ""
            let userName = dataDict["userName"] as? String ?? ""

            self.moneybel.text = MCString_judgemantDecimalsIsZero(figure: money.intValue)
            self.storeNameView.detailLabel.text = storeName
            self.storerView.detailLabel.text = nickName
            self.storerPhoneView.detailLabel.text = userName

            
            self.isPlaySwitch.isOn = !closeBroadcast.boolValue
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "店长后台"
        self.view.backgroundColor = UIColor.white
        
        
        navigationBarBackgroundImage = UIImage.init(color: UIColor.MCMain)
        navigationBarLineHidden = true
        navigationBarTitleColor = UIColor.white
        
        
        navigationItem.leftBarButtonItem = MCSetBarButtonItem_image( #imageLiteral(resourceName: "Reuse_back").MCImageWithTintColor(color: UIColor.white), target: self, selector: #selector(leftItemClicked),isLeft: true)
    }
    
    func initUI() {
        
        view.addSubview(topBgView)
        topBgView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(view)
            make.height.equalTo(150)
        }
        
        topBgView.addSubview(scanButton)
        scanButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(topBgView).offset(30)
            make.centerY.equalTo(topBgView)
            make.size.equalTo(CGSize.init(width: 40, height: 70))
        }
        
        topBgView.addSubview(billButton)
        billButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(topBgView).offset(-30)
            make.centerY.equalTo(topBgView)
            make.size.equalTo(CGSize.init(width: 40, height: 70))
        }

        
        topBgView.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(topBgView)
            make.bottom.equalTo(scanButton).offset(-5)
        }
        
        topBgView.addSubview(moneybel)
        moneybel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(topBgView)
            make.top.equalTo(scanButton).offset(2)
        }
        

        view.addSubview(storeNameView)
        storeNameView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(40)
            make.top.equalTo(topBgView.snp.bottom).offset(10)
        }
        
        view.addSubview(storerView)
        storerView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(40)
            make.top.equalTo(storeNameView.snp.bottom).offset(10)
        }

        
        view.addSubview(storerPhoneView)
        storerPhoneView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(40)
            make.top.equalTo(storerView.snp.bottom).offset(10)
        }

        view.addSubview(playView)
        playView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(40)
            make.top.equalTo(storerPhoneView.snp.bottom).offset(10)
        }
        
        view.addSubview(isPlaySwitch)
        isPlaySwitch.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(playView)
            make.right.equalTo(playView).offset(-10)
        }
        
        isPlaySwitch.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
    }
    
    // MARK: - Setter & Getter
    lazy var topBgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCMain
        return view
    }()
    
    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.text = "今日扫码收入"
        label.textColor = UIColor.white
        return label
    }()
    
    lazy var moneybel: UILabel = {
        let label = UILabel()
        label.font = MCFont(28)
        label.text = "¥0"
        label.textColor = UIColor.white
        return label
    }()
    
    lazy var scanButton: MCVerticalButton = {
        let button = MCVerticalButton()
        button.iconImageView.image = #imageLiteral(resourceName: "ManagerBackground_scan")
        button.nameLabel.textColor = UIColor.white
        button.nameLabel.text = "扫码收款"
        button.addTarget(self, action: #selector(scanButtonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var billButton: MCVerticalButton = {
        let button = MCVerticalButton()
        button.iconImageView.image = #imageLiteral(resourceName: "ManagerBackground_bill")
        button.nameLabel.text = "今日账单"
        button.nameLabel.textColor = UIColor.white
        button.addTarget(self, action: #selector(billButtonClicked), for: .touchUpInside)
        return button
    }()

    
    lazy var storeNameView: MCReuse_entryView = {
        let view = MCReuse_entryView()
        view.titleLabel.text = "门店名称"
        view.detailLabel.text = "xxxx"
        return view
    }()
    
    lazy var storerView: MCReuse_entryView = {
        let view = MCReuse_entryView()
        view.titleLabel.text = "店主名称"
        view.detailLabel.text = "名称"
        return view
    }()

    
    lazy var storerPhoneView: MCReuse_entryView = {
        let view = MCReuse_entryView()
        view.titleLabel.text = "店主手机号"
        view.detailLabel.text = "xxxx"
        view.isShowArrow = false
        return view
    }()

    
    lazy var playView: MCReuse_entryView = {
        let view = MCReuse_entryView()
        view.titleLabel.text = "允许收款语音播报"
        return view
    }()

    lazy var isPlaySwitch: UISwitch = {
        let s = UISwitch.init()
        s.onTintColor = UIColor.MCAssist_C
        s.addTarget(self, action: #selector(switchEvent), for: UIControlEvents.valueChanged)
        return s
    }()
}
